package org.example.service.Impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.example.config.AliyunDxConfig;
import org.example.constant.MemberConstants;
import org.example.domain.Member;
import org.example.ex.handler.BusinessException;
import org.example.mapper.MemberMapper;
import org.example.service.SendService;
import org.example.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Map;

@Service
public class SendServiceImpl implements SendService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private AliyunDxConfig aliyunDxConfig;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public void sendPhoneMsg(Map<String, Object> map) {
        //创建请求参数对象
        String phonenum = (String) map.get("phonenum");
        //生成一个随机数存放至redis
        String randomNumber = RandomUtil.randomNumbers(6);
        stringRedisTemplate.opsForValue().set(MemberConstants.PHONE_PREFIX + phonenum, randomNumber, Duration.ofMinutes(10));
        // 构造短信内容
        String message = "您的验证码是：" + randomNumber + "，有效期10分钟。";
        System.out.println( message);
    }

    //调用阿里云短信发送接口
    @Override
    public void sendAliyunPhoneMsg(Map<String, Object> map) {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(aliyunDxConfig.getAccessKeyId())
                .setAccessKeySecret(aliyunDxConfig.getAccessKeySecret());

        config.endpoint = aliyunDxConfig.getEndpoint();
        try {
            //创建客户端对象
            com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);
            //创建请求参数对象
            String phonenum = (String) map.get("phonenum");
            //生成一个随机数存放至redis
            String randomNumber = RandomUtil.randomNumbers(6);
            stringRedisTemplate.opsForValue().set(MemberConstants.PHONE_PREFIX + phonenum, randomNumber, Duration.ofMinutes(5));
            //模板参数
            String templateParam = "{\"code\":\"" + randomNumber + "\"}";
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                    .setPhoneNumbers(phonenum)
                    .setSignName(aliyunDxConfig.getSignName())
                    .setTemplateCode(aliyunDxConfig.getTemplateCode())
                    .setTemplateParam(templateParam);
            //发送请求
            client.sendSmsWithOptions(sendSmsRequest , new com.aliyun.teautil.models.RuntimeOptions());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Boolean saveMsgPhone(Map<String, Object> map) {
        // 获取会员输入的短信验证码
        String code = (String) map.get("code");
        // 获取会员手机号码
        String phonenum = (String) map.get("phonenum");
        // 从redis中获取当前手机号码对应验证码
        String redisCode = stringRedisTemplate.opsForValue().get(MemberConstants.PHONE_PREFIX + phonenum);
        // 判断验证码是否正确
        if (!code.equals(redisCode)) {
            throw new BusinessException("请输入正确的短信验证码");
        }
        // 将会员手机号码更新到会员信息中
        Member member = new Member();
        member.setUserMobile(phonenum);
        // 获取当前会员的openId
        String openId = AuthUtils.getMemberOpenId();

        return memberMapper.update(member, new LambdaUpdateWrapper<Member>().eq(Member::getOpenId, openId)) > 0;
    }


}
